﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SSS.handler
{
    /// <summary>
    /// AccountHandler1 的摘要说明
    /// </summary>
    public class AccountHandler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            // 取消浏览器缓存
            Common.Tools.SetPageNoCache();
            // 设置返回类型
            //context.Response.ContentType = "text/plain";
            context.Response.ContentType = "application/json";
            context.Response.Charset = "utf-8";

            #region 请求响应
            switch (context.Request["cmd"].ToLower())
            {
                case "login":               // 登录
                    context.Response.Write(login(context.Request["id"], context.Request["pwd"], context.Request.ServerVariables["remote_addr"]));
                    break;
                case "logout":              // 注销
                    context.Response.Write(logout());
                    break;
                case "getloggeduser":       // 提取当前登录的用户信息
                    context.Response.Write(getLoggedUser());
                    break;
                case "update":              // 更新用户资料
                    //context.Response.Write(updateUserInfo(key));
                    break;
                case "changepwd":    // 更改密码
                    context.Response.Write(changePwd("admin", "admin", context.Request.ServerVariables["remote_addr"], context.Request["newpwd"]));
                    break;
                default:
                    break;
            }
            #endregion
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pwd"></param>
        /// <param name="ip"></param>
        /// <returns></returns>
        private string login(string id, string pwd, string ip)
        {
            if (string.IsNullOrEmpty(id))
            {
                return "{\"cmd\":\"login\",\"state\":\"失败\",\"data\":\"帐号不能为空\"}";
            }
            else if (string.IsNullOrEmpty(pwd))
            {
                return "{\"cmd\":\"login\",\"state\":\"失败\",\"data\":\"密码不能为空\"}";
            }
            else
            {
                System.Data.DataTable dt;
                switch (SSS.DAL.AccountDB.LoginHelper(id, pwd, ip, out dt))
                {
                    case Common.EnumType.ResultState.成功:
                        SSS.Core.Service.AccountService.Current =
                            new SSS.Core.Model.User(id, pwd, (string)dt.Rows[0]["user_email"], (string)dt.Rows[0]["user_role"],
                                (string)dt.Rows[0]["last_login_ip"], (DateTime)dt.Rows[0]["last_login_time"], ip);
                        return "{\"cmd\":\"login\",\"state\":\"成功\",\"data\":" + Common.JsonHelper.ToJson(dt) + "}";
                    case Common.EnumType.ResultState.密码错误或该用户不存在:
                        return "{\"cmd\":\"login\",\"state\":\"失败\",\"data\":\"密码错误或该用户不存在\"}";
                    default:
                        return "{\"cmd\":\"login\",\"state\":\"失败\",\"data\":\"未知错误\"}";
                }
            }
        }

        /// <summary>
        /// 注销
        /// </summary>
        /// <returns></returns>
        private string logout()
        {
            SSS.Core.Service.AccountService.Current = null;
            return "{\"cmd\":\"logout\",\"state\":\"已安全退出\"}";
        }

        /// <summary>
        /// 更改密码
        /// </summary>
        /// <returns></returns>
        private string changePwd(string id, string pwd, string ip, string newPwd)
        {
            switch (SSS.DAL.AccountDB.ChangePassword(id, pwd, ip, newPwd))
            {
                case Common.EnumType.ResultState.成功:
                    return "{\"cmd\":\"changepwd\",\"state\":\"成功\"}";
                default:
                    return "{\"cmd\":\"changepwd\",\"state\":\"失败\"}";
            }
        }

        /// <summary>
        /// 获取已登录的用户信息
        /// </summary>
        /// <returns></returns>
        private string getLoggedUser()
        {
            if (null != SSS.Core.Service.AccountService.Current)
            {
                return "{\"cmd\":\"getloggeduser\",\"state\":\"成功\",\"data\":" + SSS.Core.Service.AccountService.Current.ToString() + "}";
            }
            return "{\"cmd\":\"getloggeduser\",\"state\":\"失败\"}";
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}